/* example1.c                                                      */
/*                                                                 */
/* This small program shows how to print a rotated string with the */
/* FreeType 2 library.                                             */


#include <stdio.h>
#include <string.h>
#include <math.h>
#include<wchar.h>
#include <locale.h>

#include "/usr/include/freetype2/ft2build.h"
#include FT_FREETYPE_H


#define WIDTH   2000
#define HEIGHT  480


/* origin is the upper left corner */
unsigned char image[HEIGHT][WIDTH];

FILE *f_img;


/* Replace this function with something useful. */

void draw_bitmap( FT_Bitmap*  bitmap, FT_Int x, FT_Int y)
{
    FT_Int  i, j, p, q;
    FT_Int  x_max = x + bitmap->width;
    FT_Int  y_max = y + bitmap->rows;


    /* for simplicity, we assume that `bitmap->pixel_mode' */
    /* is `FT_PIXEL_MODE_GRAY' (i.e., not a bitmap font)   */

    for ( i = x, p = 0; i < x_max; i++, p++ )
    {
        for ( j = y, q = 0; j < y_max; j++, q++ )
        {
            if ( i < 0      || j < 0       ||
                i >= WIDTH || j >= HEIGHT )
            continue;

            image[j][i] |= bitmap->buffer[q * bitmap->width + p];
        }
    }
}


void show_image( void )
{
    int  i, j;


    for ( i = 0; i < HEIGHT; i++ )
    {
        for ( j = 0; j < WIDTH; j++ )
        putchar( image[i][j] == 0 ? ' '
                : image[i][j] < 128 ? '+'
                : '*' );
        putchar( '\n' );
    }
}

wchar_t *to_wchar(wchar_t **ppDest, const char *pSrc)
{

    int len = 0;
    int ret = 0;

    len = strlen(pSrc);
    printf("line: %d  len=%d\n", __LINE__, len);
    if (len <= 1) return *ppDest;

    *ppDest = (wchar_t*)malloc (len*sizeof(wchar_t));

    /*如果第一个参数为NULL,则可以用来返回宽字符的长度*/
    printf ("len:%ld\n", mbstowcs(NULL,pSrc,0)+1);

    ret = mbstowcs(*ppDest, pSrc, len);

    return *ppDest;
}

int main( int     argc, char**  argv )
{
    FT_Library    library;
    FT_Face       face;

    FT_GlyphSlot  slot;
    FT_Matrix     matrix;                 /* transformation matrix */
    FT_Vector     pen;                    /* untransformed origin  */
    FT_Error      error;

    char*         filename;
    char*         text;

    double        angle;
    int           target_height;
    int           n, num_chars;

    wchar_t       *wchar_text;
    int            wchar_len;


    if ( argc != 3 )
    {
        fprintf ( stderr, "usage: %s font sample-text\n", argv[0] );
        exit( 1 );
    }
    // 这个很重要
    setlocale(LC_CTYPE, "zh_CN.utf8");

    printf("before fopen()\n");
    f_img = fopen("image.raw", "wb");
    printf("after fopen()\n");
    filename      = argv[1];                           /* first argument     */
    text          = argv[2];                           /* second argument    */
    num_chars     = strlen( text );
    angle         = ( 0 / 360 ) * 3.14159 * 2;      /* use 25 degrees     */
    target_height = HEIGHT;

    to_wchar(&wchar_text, text);
    wchar_len = wcslen(wchar_text);

    error = FT_Init_FreeType( &library );              /* initialize library */
    /* error handling omitted */

    error = FT_New_Face( library, filename, 0, &face );/* create face object */
    /* error handling omitted */

    /* use 50pt at 100dpi */
    error = FT_Set_Char_Size( face, 50 * 64, 0,
                             100, 0 );                /* set character size */
    /* error handling omitted */

    /* cmap selection omitted;                                        */
    /* for simplicity we assume that the font contains a Unicode cmap */

    slot = face->glyph;

    /* set up matrix */
    matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L );
    matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );
    matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
    matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L );

    /* the pen position in 26.6 cartesian space coordinates; */
    /* start at (300,200) relative to the upper left corner  */
    pen.x = 100 * 64;
    pen.y = ( target_height - 300 ) * 64;

    printf("wchar_len=%d\n", wchar_len);
    for ( n = 0; n < wchar_len; n++ )
    {
        /* set transformation */
        FT_Set_Transform( face, &matrix, &pen );

        /* load glyph image into the slot (erase previous one) */
        error = FT_Load_Char( face, (unsigned int)wchar_text[n], FT_LOAD_RENDER );
        if ( error )
        continue;                 /* ignore errors */

        /* now, draw to our target surface (convert position) */
        printf("bitmap_left(%d), bitmap_top(%d)\n", slot->bitmap_left, slot->bitmap_top);
        printf("bitmap_width(%d), bitmap_rows(%d)\n", slot->bitmap.width, slot->bitmap.rows);
        draw_bitmap( &slot->bitmap,
                    slot->bitmap_left,
                    target_height - slot->bitmap_top );

        /* increment pen position */
        pen.x += slot->advance.x;
        pen.y += slot->advance.y;
    }

    if (wchar_text) {
        free(wchar_text);
    }
    fwrite(image, WIDTH * HEIGHT, 1, f_img);
    fclose(f_img);

    // show_image();

    FT_Done_Face( face );
    FT_Done_FreeType( library );

    return 0;
}
/* EOF */
